<?php defined('BASEPATH') || exit('No direct script access allowed');

class Hometop_model extends BF_Model
{
    protected $table_name	= 'bf_hometop';
	protected $key			= 'id';
	protected $date_format	= 'datetime';

	protected $log_user 	= false;
	protected $set_created	= false;
	protected $set_modified = false;
	protected $soft_deletes	= true;

    protected $deleted_field     = 'deleted';

	// Customize the operations of the model without recreating the insert,
    // update, etc. methods by adding the method names to act as callbacks here.
	protected $before_insert 	= array();
	protected $after_insert 	= array();
	protected $before_update 	= array();
	protected $after_update 	= array();
	protected $before_find 	= array();
	protected $after_find 		= array('set_photo_domain');
	protected $before_delete 	= array();
	protected $after_delete 	= array();

	// For performance reasons, you may require your model to NOT return the id
	// of the last inserted row as it is a bit of a slow method. This is
    // primarily helpful when running big loops over data.
	protected $return_insert_id = true;

	// The default type for returned row data.
	protected $return_type = 'object';

	// Items that are always removed from data prior to inserts or updates.
	protected $protected_attributes = array();

	// You may need to move certain rules (like required) into the
	// $insert_validation_rules array and out of the standard validation array.
	// That way it is only required during inserts, not updates which may only
	// be updating a portion of the data.
	protected $validation_rules 		= array(
		array(
			'field' => 'city',
			'label' => 'lang:hometop_field_city',
			'rules' => 'max_length[11]',
		),
		array(
			'field' => 'imgtype',
			'label' => 'lang:hometop_field_imgtype',
			'rules' => 'max_length[2]',
		),
		array(
			'field' => 'imgroute',
			'label' => 'lang:hometop_field_imgroute',
			'rules' => 'max_length[225]',
		),
		array(
			'field' => 'deleted',
			'label' => 'lang:hometop_field_deleted',
			'rules' => 'max_length[2]',
		),
	);
	protected $insert_validation_rules  = array();
	protected $skip_validation 			= false;

    /**
     * Constructor
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }
    //格式化post过来的数据
    public function prep_data($post_data) {
        $data = parent::prep_data($post_data);

        $url = $post_data['url'];
        if (is_array($url)) {
            $platform = $post_data['platform'];
            $city = $post_data['city_id'];
            $banner_url = $post_data['banner_url'];
            // 整理新增数据
            $data = [];
            foreach ($url as $key => $val) {
                $data[] = array('imgroute'=>$val,  'imgtype'=>$platform[$key],'city'=>$city,'url'=>$banner_url);
            }

             return $data;
        }else{
            $info['imgroute']=$post_data['url'];
            $info['imgtype']=$post_data['platform'];
            $info['city']=$post_data['city_id'];
            $info['url'] = $post_data['banner_url'];
            return $info;
        }

       
    }
    /**
     * 添加多个图片
     *
     * @param array $data 数据
     * @return mixed 成功返回添加的条数，否则返回false
     */
    public function addPhotos($data) {
        $return = false;
        if (is_array($data)) {
            $return = $this->db->insert_batch($this->table_name, $data);
        }
        return $return;
    }
        /**
     * 设置图片域名
     * @param object $row
     */
    protected function set_photo_domain($row) {
        if (is_object($row)) {
            $domain = is_fang90_server_name() ? config_item('photo.fang90.domain') : config_item('photo.domain');
            $row->absolute_url = $domain.$row->imgroute;
            if (is_plusfang_server_name()) {
                $row->absolute_url = config_item('photo.plusfang.domain').$row->imgroute;
            }
        }
        return $row;
    }
}